package com.hgl.bi.web.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: BI
 * @description: rabbitmq配置
 * @author: hgl
 * @create: 2025-02-15 22:29
 */
@Configuration
public class RabbitConfig {

    // 主队列配置
    public static final String DATA_CLEAN_QUEUE = "data.clean.queue";
    public static final String DATA_CLEAN_EXCHANGE = "data.clean.exchange";
    public static final String DATA_CLEAN_ROUTING_KEY = "data.clean.routing";

    public static final String DATA_ANALYZE_QUEUE = "data.analyze.queue";
    public static final String DATA_ANALYZE_EXCHANGE = "data.analyze.exchange";
    public static final String DATA_ANALYZE_ROUTING_KEY = "data.analyze.routing";
    // 死信队列配置
    public static final String DLX_QUEUE = "dlx.process.queue";
    public static final String DLX_EXCHANGE = "dlx.process.exchange";
    public static final String DLX_ROUTING_KEY = "dlx.process.routing";

    @Bean
    public Queue dataCleanQueue() {
        return QueueBuilder.durable(DATA_CLEAN_QUEUE)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY)
                .build();
    }

    @Bean
    public DirectExchange dataCleanExchange() {
        return new DirectExchange(DATA_CLEAN_EXCHANGE);
    }

    @Bean
    public Binding dataCleanBinding() {
        return BindingBuilder.bind(dataCleanQueue())
                .to(dataCleanExchange())
                .with(DATA_CLEAN_ROUTING_KEY);
    }

    @Bean
    public Queue dataAnalyzeQueue() {
        return QueueBuilder.durable(DATA_ANALYZE_QUEUE)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY)
                .build();
    }

    @Bean
    public DirectExchange dataAnalyzeExchange() {
        return new DirectExchange(DATA_ANALYZE_EXCHANGE);
    }

    @Bean
    public Binding dataAnalyzeBinding() {
        return BindingBuilder.bind(dataAnalyzeQueue())
                .to(dataAnalyzeExchange())
                .with(DATA_ANALYZE_ROUTING_KEY);
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue(DLX_QUEUE, true);
    }

    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE);
    }

    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue())
                .to(dlxExchange())
                .with(DLX_ROUTING_KEY);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        template.setMandatory(true); // 开启消息到达确认
        return template;
    }
}
